package cn.trigram.mgb.ext;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.config.ColumnOverride;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.internal.util.JavaBeansUtil;

public abstract class AbstractTableGenerateCustomize {

  protected Context context;

  protected TableConfiguration tableConfiguration;

  public void setContext(Context context) {

    this.context = context;
  }

  public void setTableConfiguration(TableConfiguration tableConfiguration) {

    this.tableConfiguration = tableConfiguration;
  }

  public abstract void configProperty();

  public abstract String getMapperName();

  public abstract String getDomainObjectName();

  public abstract String getTablePrefix();

  public abstract String getTableSuffix();

  public void configDefaultSettings() {

  }

  public List<String> getDatabaseTables() {

    return null;
  }

  public void customizeColumn() {

  }

  public ResultSet getTables(DatabaseMetaData databaseMetaData) throws SQLException {

    return databaseMetaData.getTables(null, null, null, null);
  }

  public abstract String getJavaProperty(IntrospectedColumn introspectedColumn);

  protected void addColumnOverride(
      TableConfiguration tableConfiguration, String columnName, String jdbcType, String javaProperty, String javaType
  ) {

    ColumnOverride columnOverride = new ColumnOverride(columnName);
    columnOverride.setJdbcType(jdbcType);
    columnOverride.setJavaProperty(javaProperty);
    columnOverride.setJavaType(javaType);
    tableConfiguration.addColumnOverride(columnOverride);
    ColumnOverride columnOverride2 = new ColumnOverride(StringUtils.upperCase(columnName));
    columnOverride.setJdbcType(jdbcType);
    columnOverride.setJavaProperty(javaProperty);
    columnOverride.setJavaType(javaType);
    tableConfiguration.addColumnOverride(columnOverride2);
  }

  public static class DefaultTableGenerateCustomize extends AbstractTableGenerateCustomize {


    @Override
    public void configDefaultSettings() {

      tableConfiguration.setWildcardEscapingEnabled(true);
    }

    @Override
    public String getJavaProperty(IntrospectedColumn introspectedColumn) {

      return JavaBeansUtil.getCamelCaseString(introspectedColumn.getActualColumnName(), false);
    }

    @Override
    public void configProperty() {

      tableConfiguration.addProperty("escapeWildcards", "true");
      tableConfiguration.addProperty("ignoreQualifiersAtRuntime", "true");
      tableConfiguration.addProperty("modelOnly", "false");
    }

    @Override
    public String getMapperName() {

      final String entityClassName = JavaBeansUtil.getCamelCaseString(StringUtils.removeEndIgnoreCase(
          StringUtils.removeStartIgnoreCase(tableConfiguration.getTableName(), this.getTablePrefix()),
          this.getTableSuffix()
      ), true);
      return entityClassName + "Mapper";
    }

    @Override
    public String getDomainObjectName() {

      final String entityClassName = JavaBeansUtil.getCamelCaseString(StringUtils.removeEndIgnoreCase(
          StringUtils.removeStartIgnoreCase(tableConfiguration.getTableName(), this.getTablePrefix()),
          this.getTableSuffix()
      ), true);
      return entityClassName + "Entity";
    }

    @Override
    public String getTablePrefix() {

      return "";
    }

    @Override
    public String getTableSuffix() {

      return "";
    }

  }

}
